package com.xju.service;

import com.wjx.mybatis.util.MyBatisSessionUtil;
import com.xju.dao.UserMapper;
import com.xju.entity.User;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserService {
    @Autowired
    private MyBatisSessionUtil sessionUtil;

    public void save(User u) {
        SqlSession session = sessionUtil.getSession();
        try {
            UserMapper mapper = session.getMapper(UserMapper.class);
            mapper.save(u);
            session.commit();
        } catch(Exception e) {
            e.printStackTrace();
        } finally {
            sessionUtil.closeSession();
        }
    }

    public List<User> findAll() {
        SqlSession session = sessionUtil.getSession();
        try {
            UserMapper mapper = session.getMapper(UserMapper.class);
            List<User> all = mapper.findAll();
            if(all == null) {
                System.out.println("all is null");
            }
            return all;
        } catch(Exception e) {
            e.printStackTrace();
        } finally {
            sessionUtil.closeSession();
        }
        return null;
    }
}
